package com.cw.japp

import grails.plugin.springsecurity.annotation.Secured

class HomeController {
	
	def springSecurityService
	@Secured(['ROLE_USER'])
    def index() {
		redirect(action: "user", params: params)
	}
	
	@Secured(['ROLE_USER'])
	def user(){
		def userDetails = springSecurityService.principal
		def user = User.get(userDetails.id);
		def role = Role.findByAuthority("ROLE_MODEL")
		def models = UserRole.findAllByRole(role).user
		render(view : "/landing/show", model : [userInstance: user, models:models])
	}
	
	@Secured('ROLE_USER')
	def profilePreview(){
		def userDetails = springSecurityService.principal
		def user = User.get(userDetails.id)
		def roles = user.getAuthorities()
		def isModel = false
		roles.each { role ->
			if(role.authority == 'ROLE_MODEL'){
				isModel = true
			}
		}
		def modelProfile = UserProfile.findByUser(user)
		[userInstance: user, modelInstance: user, modelProfile: modelProfile, isModel: isModel]
	}
	
	@Secured('permitAll')
	def profileView(){
		def userDetails
		def user
		if(springSecurityService.isLoggedIn()){
			userDetails = springSecurityService.principal
			user = User.get(userDetails.id);
		}else{
			user = new User(username : 'Guest', firstName: "Guest", lastName: "123")
		}
		def modelIns = User.get(params.modelName)
		def modelProfile = UserProfile.findByUser(modelIns)
		render(view : "/home/profilePreview", model : [userInstance: user, modelInstance: modelIns, modelProfile: modelProfile, isModel: true])
	}
	
	@Secured(['permitAll'])
	def completeProfile(){
		def userDetails = springSecurityService.principal
		def user = User.get(userDetails.id);
		def userProfileDetails = UserProfile.findByUser(user)
		def userProfile;
		
		def modelIns = User.get(params.modelName);
		[userInstance: user, modelInstance: modelIns, userProfile : userProfileDetails ]
	}
	
	@Secured(['ROLE_USER',"ROLE_MODEL"])
	def completeProfileProcess(){
		
		/* 
		 * 
		 *  profileHeadline:, :, :, 
		 * :, =null, :, , country:, city */
		def userDetails = springSecurityService.principal;
		def user = User.get(userDetails.id);
		
		def userProfile = UserProfile.findByUser(user)
		
		if(!userProfile){
		userProfile = new UserProfile(gender : params.gender, sexualPref : params.sexualPreferences, maritalStatus : params.maritalStatus, ethinicity : params.ethinicity,
			eyeColor : params.eyesColor, hair : params.hair, bodyType : params.bodyType, weight : params.weight, height : params.height, weightUnit : params.weightUnits,
			heightUnit : params.heightUnits, smoke : params.smoke, drink : params.drink, drugs : params.drugs, occupation : params.occupation, school : params.schoolOrCollegeName,
			favFood : params.favouriteFood, pets : params.pets, cars : params.cars, talent : params.talent,
			perfectMate : params.perfectMate, turnOnsOffs : params.turnOffAndOn,
			perfectDate : params.perfectDate, meaningOfLife : params.meaningOfLife, favoriteBooks : params.favoriteBooks, favoriteMovies : params.favoriteMovie,
			favoriteMusic : params.favoriteMusic, hobbies : params.hobbies,
			craziestThingEverDone : params.craziestThingDone, anywhereRightNow : params.wishPlace,
			fiveThingsCannotLiveWithout : params.cantLiveWithout, doForFun : params.doForFun, reasonToKnowMe : params.reasonToKnowMe, moreAboutMe : params.abtMe, birthDate: params.dob, headline: params.profileHeadline, city: params.city, country: params.country);
		
		userProfile.user = user;
		}else{
		userProfile.properties = [gender : params.gender, sexualPref : params.sexualPreferences, maritalStatus : params.maritalStatus, ethinicity : params.ethinicity,
			eyeColor : params.eyesColor, hair : params.hair, bodyType : params.bodyType, weight : params.weight, height : params.height, weightUnit : params.weightUnits,
			heightUnit : params.heightUnits, smoke : params.smoke, drink : params.drink, drugs : params.drugs, occupation : params.occupation, school : params.schoolOrCollegeName,
			favFood : params.favouriteFood, pets : params.pets, cars : params.cars, talent : params.talent,
			perfectMate : params.perfectMate, turnOnsOffs : params.turnOffAndOn,
			perfectDate : params.perfectDate, meaningOfLife : params.meaningOfLife, favoriteBooks : params.favoriteBooks, favoriteMovies : params.favoriteMovie,
			favoriteMusic : params.favoriteMusic, hobbies : params.hobbies,
			craziestThingEverDone : params.craziestThingDone, anywhereRightNow : params.wishPlace,
			fiveThingsCannotLiveWithout : params.cantLiveWithout, doForFun : params.doForFun, reasonToKnowMe : params.reasonToKnowMe, moreAboutMe : params.abtMe, birthDate: params.dob, headline: params.profileHeadline, city: params.city, country: params.country ];
		}
		
		userProfile = userProfile.save();
		
		/*Date birthDate : dob_month, dob_year, dob_day
		
		Integer status
		Date lastModifiedDate
		Date createdDate*/
		def roles = user.getAuthorities()
		def isModel = false
		roles.each { role ->
			if(role.authority == 'ROLE_MODEL'){
				isModel = true
			}
		}
		render(view : "profilePreview", model : [userInstance: user, modelInstance: user, modelProfile: userProfile, isModel: isModel])
	}
	
	@Secured('ROLE_USER')
	def profileImageUpload() {
		String profilePicturePath = params.pictureUri;
		if(profilePicturePath)
		{
			File file = new File(profilePicturePath)
			response.contentType = URLConnection.guessContentTypeFromName(file.getName())
			response.outputStream << file.bytes
			response.outputStream.flush()
		}
	}
}
